<?php

class Erra_Utils_Installer {

    public static function showWelcomeMessage($buffer) {
        if (empty($buffer)) {
            $rows = Erra_Core_DatabasePdo::_SELECT('SELECT id FROM ufm__pages WHERE page_url="default"');
            if (!Erra_Core_DatabasePdo::rowCount()) {
                self::check_ini_integrity();
                self::updateHtaccess();
                echo self::welcomeHtml();
                die();
            }
        }
    }

    public static function updateHtaccess() {
// Change htaccess if necessary
        $relativeWebrootUriPart = '';
        if (config_get('application.http_host') != config_get('application.http_webroot')) {
            $relativeWebrootUriPart = str_replace(config_get("application.http_host"), '', config_get("application.http_webroot"));
            $relativeWebrootUriPart = (strpos($relativeWebrootUriPart, '\\') == 0) ? substr($relativeWebrootUriPart, 1) : $relativeWebrootUriPart;
        }
        $htaccess = '# ERRA FRAMEWORK HTACCESS
Options +SymLinksIfOwnerMatch
AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
	RewriteEngine on
	RewriteBase /
	# XML AJAX Requests
	RewriteRule ^POST_XMLHttpRequest/(.*)$ ' . $relativeWebrootUriPart . '/index.php?/POST_XMLHttpRequest/$1 [L]
        # Rewrite _escaped_fragment
        RewriteCond %{QUERY_STRING} ^_escaped_fragment_=([^&]*)
        RewriteRule .* ' . $relativeWebrootUriPart . '/%1? [R=301,L]
        # Supress multiple slashes
        RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
        RewriteRule . %1/%2 [R=301,L]
	# Default
	RewriteRule ^([a-zA-Z0-9-_\[\]/]+)$ ' . $relativeWebrootUriPart . '/index.php?/$1 [L]
	RewriteRule ^([a-zA-Z0-9-_\[\]/]+)\.csv$ index.php?/$1 [L]
</IfModule>

<IfModule mod_deflate.c>
    <FilesMatch "\.(css|js)$">
        SetOutputFilter DEFLATE
    </FilesMatch>
</IfModule>

<IfModule mod_expires.c>  
    ExpiresActive On  
    ExpiresDefault "access plus 7200 seconds"  
    ExpiresByType image/jpg "access plus 2592000 seconds"  
    ExpiresByType image/jpeg "access plus 2592000 seconds"  
    ExpiresByType image/png "access plus 2592000 seconds"  
    ExpiresByType image/gif "access plus 2592000 seconds"  
    AddType image/x-icon .ico  
    ExpiresByType image/ico "access plus 2592000 seconds"  
    ExpiresByType image/icon "access plus 2592000 seconds"  
    ExpiresByType image/x-icon "access plus 2592000 seconds"  
    ExpiresByType text/css "access plus 2592000 seconds"  
    ExpiresByType text/javascript "access plus 2592000 seconds"  
    ExpiresByType text/html "access plus 7200 seconds"  
    ExpiresByType application/xhtml+xml "access plus 7200 seconds"  
    ExpiresByType application/javascript A259200  
    ExpiresByType application/x-javascript "access plus 2592000 seconds"  
    ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"  
</IfModule>  

<IfModule mod_headers.c>  
    <FilesMatch "\\.(ico|jpe?g|png|gif|swf)$">  
        Header set Cache-Control "max-age=2592000, public"  
    </FilesMatch>  
    <FilesMatch "\\.(css)$">  
        Header set Cache-Control "max-age=604800, public"  
    </FilesMatch>  
    <FilesMatch "\\.(js)$">  
        Header set Cache-Control "max-age=216000, private"  
    </FilesMatch>  
    <FilesMatch "\\.(x?html?|php)$">  
        Header set Cache-Control "max-age=600, private, must-revalidate"  
    </FilesMatch>  
</IfModule>';
        $current_htaccess = file_get_contents(PUBLIC_ROOT . '/.htaccess');
        if ($current_htaccess != $htaccess) {
            file_put_contents(PUBLIC_ROOT . '/.htaccess', $htaccess);
        }
    }

    public static function check_ini_integrity() {
        Erra_Core_Config::check_ini();
// Check directories
        $directoryToCheck = array('public_images', 'public_style', 'public_js', 'public_upload_directory', 'framework_images', 'framework_style', 'framework_js');
        foreach ($directoryToCheck as $directory) {
            if (!is_dir(Erra_Core_File::web2path(config_get($directory)))) {
                Erra_Core_Kernel::fatalError('The parameter `path.' . $directory . '` with value `' . config_get($directory) . '` in ' . INI_FILE . ' is not a valid directory');
            }
        }
// Check login and passwords
        $login_errors = '';
        $login_to_check = array('access_control.dev_login', 'access_control.dev_password', 'access_control.admin_login', 'access_control.admin_password');
        foreach ($login_to_check as $parameter) {
            if (config_get($parameter) == 'dev' || config_get($parameter) == 'admin') {
                $login_errors .= '<div style="padding-bottom:5px;">' . __('The parameter') . ' `' . $parameter . '` ' . __('has value') . ' `' . config_get($parameter) . '` ' . __('in') . ' ' . INI_FILE . '. ' . __('This is the default value and you must change this parameter') . '</div>';
            }
        }
        if (strlen($login_errors)) {
            Erra_Core_Kernel::fatalError($login_errors);
        }
    }

    public static function welcomeHtml() {
// Display's welcome message
        $res .= '<html>';
        $res .= '<head>';
        $res .= '<style type="text/css">';
        $res .= 'body { font-family:arial, sans-serif; padding15px; }h2 { font-size:20px; } ';
        $res .= '</style>';
        $res .= '</head>';
        $res .= '<center>';
        $ini_file = '<img src="' . config_get('path.framework_images') . '/install/ini.png" />';
        $res .= '<h2>' . $ini_file . ' <font color="green">' . INI_FILE . '</font> ' . __('has been setup correctly') . '</h2>';
        $framework_file = '<img src="' . config_get('path.framework_images') . '/install/erra-framework-logo.png" />';
        $res .= '<table><tr><td>' . $framework_file . '</td><td style="padding-left:20px;"><h1>' . __('is ready to run on your server') . '</h1></td></tr></table>';
        $res .= '<img src="' . config_get('path.framework_images') . '/install/success.png" /><br/>';
        $res .= __('You are seeing this page because you have correctly installed the Erra Framework and the .ini file');
        $res .= '<br/><br/>';
        $res .= __('To start using the framework, go to ');
        $res .= '<a href="' . config_get('application.http_webroot') . '/config">the configuration panel</a><br/>';
        $res .= __('Create a new page, set URL route to `default`. Create and choose a layout, and start build your application !');
        $res .= '<br/><br/>';
        $res .= __('A quick reminder of your developper access');
        $res .= '<br/><img src="' . config_get('path.framework_images') . '/install/password.png" /><br/>';
        $res .= __('Login : ') . '<b>' . config_get('access_control.dev_login') . '</b>';
        $res .= '<br/>';
        $res .= __('Password : ') . '<b>' . config_get('access_control.dev_password') . '</b>';


        $res .= '</center>';
        return $res;
    }

}

?>